package ru.dc.gui;

import ru.dc.object.DownloadItem;
import ru.dc.object.DownloadItemStatus;
import ru.dc.object.ServerInfo;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 03.11.2010
 * Time: 10:29:44
 * </pre>
 */
public class DownloadsTableModel extends AbstractTableModel {

    private String[] columnNames = new String[] {
            "Файл",
            "Скачано",
            "Размер",
            "Статус",
            "Пользователь",
            "Хаб"
    };
    private List<DownloadItem> downloads = new ArrayList<DownloadItem>();
    private Map<DownloadItem, DownloadItemStatus> downloadStatus = new HashMap<DownloadItem, DownloadItemStatus>();
    private Map<DownloadItem, Long> downloaded = new HashMap<DownloadItem, Long>();


    public int getRowCount() {
        return downloads.size();
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 1 : return Long.class;
            case 2 : return Long.class;
            case 3 : return DownloadItemStatus.class;
            case 5 : return ServerInfo.class;
            default: return String.class;
        }
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        final DownloadItem item = downloads.get(rowIndex);
        switch (columnIndex) {
            case 0 : return item.getFilename();
            case 1 : return downloaded.get(item);
            case 2 : return item.getLength();
            case 3 : return downloadStatus.get(item);
            case 4 : return item.getSource().getNick();
            case 5 : return item.getSource().getHub();
            default: return String.class;
        }
    }

    public void addDownloadItem(DownloadItem item) {
        downloads.add(item);
        downloadStatus.put(item, DownloadItemStatus.WAITING);
        fireTableRowsInserted(downloads.size()-1, downloads.size()-1);
    }

    public void removeDownloadItem(DownloadItem item) {
        int index = downloads.indexOf(item);
        if (index>=0) {
            downloads.remove(item);
            downloadStatus.remove(item);
            fireTableRowsDeleted(index, index);
        }
    }

    public void clear() {
        final int size = downloads.size();
        if (size > 0) {
            downloads.clear();
            downloadStatus.clear();
            fireTableRowsDeleted(0, size-1);
        }
    }

    public void updateDownloadStatus(DownloadItem item, DownloadItemStatus status) {
        if (item==null) {
            return;
        }
        int index = downloads.indexOf(item);
        if (index>=0) {
            downloadStatus.put(item, status);
            fireTableCellUpdated(index, 3);
        }
    }

    public void updateDownloaded(DownloadItem item, long downloaded) {
        if (item==null) {
            return;
        }
        int index = downloads.indexOf(item);
        if (index>=0) {
            this.downloaded.put(item, downloaded);
            fireTableCellUpdated(index, 1);
        }
    }

    /**
     * Проставляет указанный статус всем элементам загрузки, у которых совпадает адрес клиента
     * @param clientConnection адрес клиента
     * @param status статус
     */
    public void updateDownloadsStatus(ServerInfo clientConnection, DownloadItemStatus status) {
        for (DownloadItem item : downloads) {
            int index = downloads.indexOf(item);
            if (item.getSource().getHub().equals(clientConnection)) {
                downloadStatus.put(item, status);
                fireTableCellUpdated(index, 2);
            }
        }
    }
}
